home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / UXFS.C < prev    next >
C/C++ Source or Header  |  1992-02-08  |  9KB  |  354 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /sw/cph/scheme1/src/microcode/RCS/uxfs.c,v 1.7 1992/02/09 03:41:48 cph Exp $
  4.  
  5. Copyright (c) 1990-92 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. #include "ux.h"
  36. #include "osfs.h"
  37.  
  38. int
  39. DEFUN (UX_read_file_status, (filename, s),
  40.        CONST char * filename AND
  41.        struct stat * s)
  42. {
  43.   while ((UX_lstat (filename, s)) < 0)
  44.     {
  45.       if (errno == EINTR)
  46.     continue;
  47.       if ((errno == ENOENT) || (errno == ENOTDIR))
  48.     return (0);
  49.       error_system_call (errno, syscall_lstat);
  50.     }
  51.   return (1);
  52. }
  53.  
  54. int
  55. DEFUN (UX_read_file_status_indirect, (filename, s),
  56.        CONST char * filename AND
  57.        struct stat * s)
  58. {
  59.   while ((UX_stat (filename, s)) < 0)
  60.     {
  61.       if (errno == EINTR)
  62.     continue;
  63.       if ((errno == ENOENT) || (errno == ENOTDIR))
  64.     return (0);
  65.       error_system_call (errno, syscall_stat);
  66.     }
  67.   return (1);
  68. }
  69.  
  70. enum file_existence
  71. DEFUN (OS_file_existence_test, (name), CONST char * name)
  72. {
  73.   struct stat s;
  74.   return
  75.     ((UX_read_file_status_indirect (name, (&s)))
  76.      ? file_does_exist
  77.      : (UX_read_file_status (name, (&s)))
  78.      ? file_is_link
  79.      : file_doesnt_exist);
  80. }
  81.  
  82. int
  83. DEFUN (OS_file_directory_p, (name), CONST char * name)
  84. {
  85.   struct stat s;
  86.   return
  87.     ((UX_read_file_status_indirect (name, (&s)))
  88.      && (((s . st_mode) & S_IFMT) == S_IFDIR));
  89. }
  90.  
  91. CONST char *
  92. DEFUN (OS_file_soft_link_p, (name), CONST char * name)
  93. {
  94. #ifdef HAVE_SYMBOLIC_LINKS
  95.   struct stat s;
  96.   if (! ((UX_read_file_status (name, (&s)))
  97.      && (((s . st_mode) & S_IFMT) == S_IFLNK)))
  98.     return (0);
  99.   {
  100.     int scr;
  101.     int buffer_length = 100;
  102.     char * buffer = (UX_malloc (buffer_length));
  103.     if (buffer == 0)
  104.       error_system_call (ENOMEM, syscall_malloc);
  105.     while (1)
  106.       {
  107.     STD_UINT_SYSTEM_CALL
  108.       (syscall_readlink, scr, (UX_readlink (name, buffer, buffer_length)));
  109.     if (scr < buffer_length)
  110.       break;
  111.     buffer_length *= 2;
  112.     buffer = (UX_realloc (buffer, buffer_length));
  113.     if (buffer == 0)
  114.       error_system_call (ENOMEM, syscall_realloc);
  115.       }
  116.     (buffer[scr]) = '\0';
  117.     return ((CONST char *) buffer);
  118.   }
  119. #else
  120.   return (0);
  121. #endif
  122. }
  123.  
  124. int
  125. DEFUN (OS_file_access, (name, mode), CONST char * name AND unsigned int mode)
  126. {
  127.   return ((UX_access (name, mode)) == 0);
  128. }
  129.  
  130. void
  131. DEFUN (OS_file_remove, (name), CONST char * name)
  132. {
  133.   STD_VOID_SYSTEM_CALL (syscall_unlink, (UX_unlink (name)));
  134. }
  135.  
  136. void
  137. DEFUN (OS_file_remove_link, (name), CONST char * name)
  138. {
  139.   struct stat s;
  140.   if ((UX_read_file_status (name, (&s)))
  141.       && ((((s . st_mode) & S_IFMT) == S_IFREG)
  142. #ifdef HAVE_SYMBOLIC_LINKS
  143.       || (((s . st_mode) & S_IFMT) == S_IFLNK)
  144. #endif
  145.       ))
  146.     UX_unlink (name);
  147. }
  148.  
  149. void
  150. DEFUN (OS_file_link_hard, (from_name, to_name),
  151.        CONST char * from_name AND
  152.        CONST char * to_name)
  153. {
  154.   STD_VOID_SYSTEM_CALL (syscall_link, (UX_link (from_name, to_name)));
  155. }
  156.  
  157. void
  158. DEFUN (OS_file_link_soft, (from_name, to_name),
  159.        CONST char * from_name AND
  160.        CONST char * to_name)
  161. {
  162. #ifdef HAVE_SYMBOLIC_LINKS
  163.   STD_VOID_SYSTEM_CALL (syscall_symlink, (UX_symlink (from_name, to_name)));
  164. #else
  165.   error_unimplemented_primitive ();
  166. #endif
  167. }
  168.  
  169. void
  170. DEFUN (OS_file_rename, (from_name, to_name),
  171.        CONST char * from_name AND
  172.        CONST char * to_name)
  173. {
  174.   STD_VOID_SYSTEM_CALL (syscall_rename, (UX_rename (from_name, to_name)));
  175. }
  176.  
  177. void
  178. DEFUN (OS_directory_make, (name), CONST char * name)
  179. {
  180.   STD_VOID_SYSTEM_CALL (syscall_mkdir, (UX_mkdir (name, MODE_DIR)));
  181. }
  182.  
  183. int OS_directory_index;
  184.  
  185. #if defined(HAVE_DIRENT) || defined(HAVE_DIR)
  186.  
  187. static DIR ** directory_pointers;
  188. static unsigned int n_directory_pointers;
  189.  
  190. void
  191. DEFUN_VOID (UX_initialize_directory_reader)
  192. {
  193.   directory_pointers = 0;
  194.   n_directory_pointers = 0;
  195.   OS_directory_index = (-1);
  196. }
  197.  
  198. static unsigned int
  199. DEFUN (allocate_directory_pointer, (pointer), DIR * pointer)
  200. {
  201.   if (n_directory_pointers == 0)
  202.     {
  203.       DIR ** pointers = ((DIR **) (UX_malloc ((sizeof (DIR *)) * 4)));
  204.       if (pointers == 0)
  205.     error_system_call (ENOMEM, syscall_malloc);
  206.       directory_pointers = pointers;
  207.       n_directory_pointers = 4;
  208.       {
  209.     DIR ** scan = directory_pointers;
  210.     DIR ** end = (scan + n_directory_pointers);
  211.     (*scan++) = pointer;
  212.     while (scan < end)
  213.       (*scan++) = 0;
  214.       }
  215.       return (0);
  216.     }
  217.   {
  218.     DIR ** scan = directory_pointers;
  219.     DIR ** end = (scan + n_directory_pointers);
  220.     while (scan < end)
  221.       if ((*scan++) == 0)
  222.     {
  223.       (*--scan) = pointer;
  224.       return (scan - directory_pointers);
  225.     }
  226.   }
  227.   {
  228.     unsigned int result = n_directory_pointers;
  229.     unsigned int n_pointers = (2 * n_directory_pointers);
  230.     DIR ** pointers =
  231.       ((DIR **)
  232.        (UX_realloc (((PTR) directory_pointers),
  233.             ((sizeof (DIR *)) * n_pointers))));
  234.     if (pointers == 0)
  235.       error_system_call (ENOMEM, syscall_realloc);
  236.     {
  237.       DIR ** scan = (pointers + result);
  238.       DIR ** end = (pointers + n_pointers);
  239.       (*scan++) = pointer;
  240.       while (scan < end)
  241.     (*scan++) = 0;
  242.     }
  243.     directory_pointers = pointers;
  244.     n_directory_pointers = n_pointers;
  245.     return (result);
  246.   }
  247. }
  248.  
  249. #define REFERENCE_DIRECTORY(index) (directory_pointers[(index)])
  250. #define DEALLOCATE_DIRECTORY(index) ((directory_pointers[(index)]) = 0)
  251.  
  252. int
  253. DEFUN (OS_directory_valid_p, (index), long index)
  254. {
  255.   return
  256.     ((0 <= index)
  257.      && (index < n_directory_pointers)
  258.      && ((REFERENCE_DIRECTORY (index)) != 0));
  259. }
  260.  
  261. unsigned int
  262. DEFUN (OS_directory_open, (name), CONST char * name)
  263. {
  264.   /* Cast `name' to non-const because hp-ux 7.0 declaration incorrect. */
  265.   DIR * pointer = (opendir ((char *) name));
  266.   if (pointer == 0)
  267.     error_system_call (errno, syscall_opendir);
  268.   return (allocate_directory_pointer (pointer));
  269. }
  270.  
  271. #ifndef HAVE_DIRENT
  272. #define dirent direct
  273. #endif
  274.  
  275. CONST char *
  276. DEFUN (OS_directory_read, (index), unsigned int index)
  277. {
  278.   struct dirent * entry = (readdir (REFERENCE_DIRECTORY (index)));
  279.   return ((entry == 0) ? 0 : (entry -> d_name));
  280. }
  281.  
  282. CONST char *
  283. DEFUN (OS_directory_read_matching, (index, prefix), 
  284.        unsigned int index AND
  285.        CONST char * prefix)
  286. {
  287.   DIR * pointer = (REFERENCE_DIRECTORY (index));
  288.   unsigned int n = (strlen (prefix));
  289.   while (1)
  290.     {
  291.       struct dirent * entry = (readdir (pointer));
  292.       if (entry == 0)
  293.     return (0);
  294.       if ((strncmp (prefix, (entry -> d_name), n)) == 0)
  295.     return (entry -> d_name);
  296.     }
  297. }
  298.  
  299. void
  300. DEFUN (OS_directory_close, (index), unsigned int index)
  301. {
  302.   closedir (REFERENCE_DIRECTORY (index));
  303.   DEALLOCATE_DIRECTORY (index);
  304. }
  305.  
  306. #else /* not HAVE_DIRENT nor HAVE_DIR */
  307.  
  308. void
  309. DEFUN_VOID (UX_initialize_directory_reader)
  310. {
  311.   OS_directory_index = (-1);
  312. }
  313.  
  314. int
  315. DEFUN (OS_directory_valid_p, (index), long index)
  316. {
  317.   return (0);
  318. }
  319.  
  320. unsigned int
  321. DEFUN (OS_directory_open, (name), CONST char * name)
  322. {
  323.   error_unimplemented_primitive ();
  324.   return (0);
  325. }
  326.  
  327. #ifndef HAVE_DIRENT
  328. #define dirent direct
  329. #endif
  330.  
  331. CONST char *
  332. DEFUN (OS_directory_read, (index), unsigned int index)
  333. {
  334.   error_unimplemented_primitive ();
  335.   return (0);
  336. }
  337.  
  338. CONST char *
  339. DEFUN (OS_directory_read_matching, (index, prefix), 
  340.        unsigned int index AND
  341.        CONST char * prefix)
  342. {
  343.   error_unimplemented_primitive ();
  344.   return (0);
  345. }
  346.  
  347. void
  348. DEFUN (OS_directory_close, (index), unsigned int index)
  349. {
  350.   error_unimplemented_primitive ();
  351. }
  352.  
  353. #endif /* HAVE_DIRENT */
  354.